package gll.pub.onepeas.controllers

import gll.pub.onepeas.models.UsersTable
import gll.pub.onepeas.repositories.UsersRepo
import gll.pub.onepeas.util.HttpResponseUtil
import gll.pub.onepeas.util.Routes
import gll.pub.onepeas.util.UUIDUtil
import kotlinx.coroutines.flow.firstOrNull
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import java.util.*

@RestController
@RequestMapping(Routes.User.USER_CONTROLLER_ENDPOINT)
class UsersController {
    @Autowired
    lateinit var usersRepo: UsersRepo

    @PostMapping(Routes.User.REGISTER)
    suspend fun register(@RequestParam phone:String, @RequestParam password:String): Any {
        val list = usersRepo.findAll()
        val user = list.firstOrNull {
            it.phone == phone
        }
        return if (user==null){
            val userInfo = usersRepo.save(UsersTable(phone = phone, password = password))
            HttpResponseUtil.response.ok(userInfo)
        }else{
            HttpResponseUtil.response.fail("当前用户已存在")
        }
    }

    @PostMapping(Routes.User.UPDATE)
    suspend fun updateUser(@RequestHeader("uuid") uuid: String, @RequestBody body: Map<String, String>): Any {
        if (UUIDUtil.UUID.isUUID(uuid)) {
            val userInfo = usersRepo.findById(UUID.fromString(uuid))
            if (userInfo!=null) {
                val userName = body["username"]
                val headImage  = body["head_image"]
                val password  = body["password"]
                val newPassword  = body["new_password"]
                val phone  = body["phone"]
                if (password!=null&&password!= userInfo.password){
                    return HttpResponseUtil.response.fail("原密码错误")
                }

                val todo = usersRepo.save(
                    UsersTable(
                        id = UUID.fromString(uuid),
                        username = userName?:userInfo.username,
                        headImage = headImage?:userInfo.headImage,
                        password = newPassword?:userInfo.password,
                        phone = phone?:userInfo.phone
                    )
                )
                return HttpResponseUtil.response.ok(todo)
            }
        }
        return HttpResponseUtil.response.fail("当前用户不存在")
    }

    @PostMapping(Routes.User.SELECT)
    suspend fun selectUser(@RequestParam("uuid") uuid: String):Any{
        if (UUIDUtil.UUID.isUUID(uuid)) {
            val userInfo = usersRepo.findById(UUID.fromString(uuid))
            if (userInfo!=null) {
                return HttpResponseUtil.response.ok(userInfo)
            }
        }
        return HttpResponseUtil.response.fail("当前用户不存在")
    }
    @PostMapping(Routes.User.USER_INFO)
    suspend fun userInfo(@RequestHeader("uuid") uuid: String):Any{
        if (UUIDUtil.UUID.isUUID(uuid)) {
            val userInfo = usersRepo.findById(UUID.fromString(uuid))
            if (userInfo!=null) {
                return HttpResponseUtil.response.ok(userInfo)
            }
        }
        return HttpResponseUtil.response.fail("当前用户不存在")
    }

    @PostMapping(Routes.User.LOGIN)
    suspend fun login(@RequestParam phone:String,@RequestParam password:String):Any{
        val list = usersRepo.findAll()
        val uuid = list.firstOrNull {
            it.phone == phone && it.password == password

        }?.id
        uuid?:return HttpResponseUtil.response.fail("用户名或密码错误")

        val userInfo = usersRepo.findById(uuid)
        return HttpResponseUtil.response.ok(userInfo!!)
    }
}